import MainHeader from '@/components/main-header/index'
import InstanceQuery from '@/apis/InstanceQuery'

import CancelEvection from '@/static/function-logo/cancel-evection.svg'
import CancelGoOut from '@/static/function-logo/cancel-go-out.svg'
import CancelVacate from '@/static/function-logo/cancel-vacate.svg'
import Certify from '@/static/function-logo/certify.svg'
import Dimission from '@/static/function-logo/dimission.svg'
import Evection from '@/static/function-logo/evection.svg'
import FillClock from '@/static/function-logo/fill-clock.svg'
import GoOut from '@/static/function-logo/go-out.svg'
import Overtime from '@/static/function-logo/overtime.svg'
import Regularization from '@/static/function-logo/regularization.svg'
import SalaryAdjustment from '@/static/function-logo/salary-adjustment.svg'
import TransferPosition from '@/static/function-logo/transfer-position.svg'
import Vacate from '@/static/function-logo/vacate.svg'

export default {
  name: 'Approval',
  components: {
    MainHeader,
  },
  mounted() {
    this.selectStateValue = this.selectStateList[
      this.$store.state.approvalSelector.iInitiated[0]
    ]
    this.selectTypeValue1 = this.selectTypeList[
      this.$store.state.approvalSelector.iInitiated[1]
    ]
    this.selectTypeValue2 = this.selectTypeList[
      this.$store.state.approvalSelector.myApproval
    ]

    // 查询我发起的
    InstanceQuery(
      {
        jobNumber: this.$store.state.userInfo.jobNumber, //工号
        processKey: '', //流程标识
        processState: this.selectStateList.indexOf(this.selectStateValue), //审批状态
        processName:
          this.selectTypeValue1 === '全部类型' ? '' : this.selectTypeValue1,
        departmentName: this.$store.state.postInfo.department,
        procCurrNodeUserGroup: '', //当前节点负责人角色
        currentPage: 1,
        pageSize: 10,
      },
      this.$store.state.authorization
    ).then((res) => {
      // console.log('查询我发起的', res.data)
      this.iInitiatedList = res.data.rows
      this.showGetMore.iInitiatedList = res.data.total > 10 ? true : false
    })

    // 查询需要审批的
    InstanceQuery(
      {
        jobNumber: '', //工号
        processKey: '', //流程标识
        processState: 0, //审批状态
        processName:
          this.selectTypeValue2 === '全部类型' ? '' : this.selectTypeValue2,
        departmentName: this.$store.state.postInfo.department,
        procCurrNodeUserGroup: this.$store.state.role, //当前节点负责人角色
        currentPage: 1,
        pageSize: 10,
      },
      this.$store.state.authorization
    ).then((res) => {
      // console.log('我的审批', res.data)
      this.myApprovalList = res.data.rows
      this.showGetMore.myApprovalList = res.data.total > 10 ? true : false
    })
  },
  data() {
    return {
      active: this.$store.state.approvalManagementTabs,
      tabsController: false,
      iInitiatedList: [],
      myApprovalList: [],
      // 选择器
      ifSelectorShow: false,
      selectStateValue: '待审批',
      selectTypeValue1: '全部类型',
      selectTypeValue2: '全部类型',
      selectDefaultValue: 0,
      selectorList: [],
      selectStateList: [
        '待审批',
        '审批中',
        '已通过',
        '已驳回',
        '已撤销',
        '待销',
      ],
      selectTypeList: [
        '全部类型',
        '请假',
        // '销假',
        '出差',
        '销除',
        // '外出',
        // '销外出',
        '加班',
        '补卡',
        '转正',
        '调岗',
        '离职',
        '调薪',
        '报销',
      ],
      signx: '',
      // 显示更多
      showGetMore: {
        iInitiatedList: false,
        myApprovalList: false,
      },
      iInitiatedCurrentPage: 1,
      myApprovalCurrentPage: 1,
    }
  },
  methods: {
    changeTabs(key) {
      if (key === 'iInitiated') {
        this.active = 0
        this.$store.commit('initApprovalManagementTabs', 0)
      } else if (key === 'myApproval') {
        this.active = 1
        this.$store.commit('initApprovalManagementTabs', 1)
      }
    },
    closeSelector() {
      this.ifSelectorShow = false
    },
    selectorCancel() {
      this.closeSelector()
    },
    selectorConfirm(event) {
      // console.log(event.mp.detail.value)
      if (this.selectorList === this.selectStateList) {
        this.selectStateValue = event.mp.detail.value
        this.selectIInitiatedList()
        this.$store.commit('initApprovalSelectorIInitiated', [
          this.selectStateList.indexOf(event.mp.detail.value),
          this.selectTypeList.indexOf(this.selectTypeValue1),
        ])
      } else if (this.selectorList === this.selectTypeList) {
        if (this.signx === 1) {
          this.selectTypeValue1 = event.mp.detail.value
          this.selectIInitiatedList()
          this.$store.commit('initApprovalSelectorIInitiated', [
            this.selectStateList.indexOf(this.selectStateValue),
            this.selectTypeList.indexOf(event.mp.detail.value),
          ])
        } else {
          this.selectTypeValue2 = event.mp.detail.value
          this.selectmyApprovalList()
          this.$store.commit(
            'initApprovalSelectorMyApproval',
            this.selectTypeList.indexOf(event.mp.detail.value)
          )
        }
      }
      this.closeSelector()
    },
    selectState() {
      this.selectorList = this.selectStateList
      this.selectDefaultValue = this.selectStateList.indexOf(
        this.selectStateValue
      )
      this.ifSelectorShow = true
    },
    selectType1() {
      this.selectorList = this.selectTypeList
      this.signx = 1
      this.selectDefaultValue = this.selectTypeList.indexOf(
        this.selectTypeValue1
      )
      this.ifSelectorShow = true
    },
    selectType2() {
      this.selectorList = this.selectTypeList
      this.signx = 2
      this.selectDefaultValue = this.selectTypeList.indexOf(
        this.selectTypeValue2
      )
      this.ifSelectorShow = true
    },
    selectIInitiatedList() {
      this.iInitiatedCurrentPage = 1
      // 筛选 发起审批
      InstanceQuery(
        {
          jobNumber: this.$store.state.userInfo.jobNumber, //工号
          processKey: '', //流程标识
          processState: this.selectStateList.indexOf(this.selectStateValue), //审批状态
          processName:
            this.selectTypeValue1 === '全部类型'
              ? ''
              : this.selectTypeValue1 === '销除'
              ? '销假、销出差'
              : this.selectTypeValue1,
          departmentName: this.$store.state.postInfo.department,
          procCurrNodeUserGroup: '', //当前节点负责人角色
          currentPage: 1,
          pageSize: 10,
        },
        this.$store.state.authorization
      ).then((res) => {
        // console.log('查询我发起的', res.data.rows)
        this.iInitiatedList = res.data.rows
        this.showGetMore.iInitiatedList = res.data.total > 10 ? true : false
      })
    },
    selectmyApprovalList() {
      this.myApprovalCurrentPage = 1
      // 筛选 我的审批
      InstanceQuery(
        {
          jobNumber: '', //工号
          processKey: '', //流程标识
          processState: 0, //审批状态
          processName:
            this.selectTypeValue2 === '全部类型'
              ? ''
              : this.selectTypeValue2 === '销除'
              ? '销假、销出差'
              : this.selectTypeValue2,
          departmentName: this.$store.state.postInfo.department,
          procCurrNodeUserGroup: this.$store.state.role, //当前节点负责人角色
          currentPage: 1,
          pageSize: 10,
        },
        this.$store.state.authorization
      ).then((res) => {
        // console.log('我的审批', res.data.rows)
        this.myApprovalList = res.data.rows
        this.showGetMore.myApprovalList = res.data.total > 10 ? true : false
      })
    },
    // 详情页
    showDetail(data) {
      uni.navigateTo({
        url:
          '/pages/main/components/approval/display-detail/index?data=' +
          JSON.stringify(data),
      })
    },
    // 日期格式化
    formDate(time) {
      let year = new Date(date).getFullYear()
      let month =
        new Date(date).getMonth() + 1 < 10
          ? '0' + (new Date(date).getMonth() + 1)
          : new Date(date).getMonth()
      let day =
        new Date(date).getDate() < 10
          ? '0' + new Date(date).getDate()
          : new Date(date).getDate()
      let hours =
        new Date(date).getHours() < 10
          ? '0' + new Date(date).getHours()
          : new Date(date).getHours()
      let minutes =
        new Date(date).getMinutes() < 10
          ? '0' + new Date(date).getMinutes()
          : new Date(date).getMinutes()
      return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes
    },
    // IOS兼容问题
    formDateIOS(date) {
      return date.split(':')[0] + ':' + date.split(':')[1]
    },
    // logo选择器
    switchApprovalLogo(processName) {
      switch (processName) {
        case '销出差':
          return CancelEvection
          break
        case '请假':
          return Vacate
          break
        case '外出':
          return GoOut
          break
        case '销假、销出差':
          return CancelVacate
          break
        case '出差':
          return Evection
          break
        case '销外出':
          return CancelGoOut
          break
        case '加班':
          return Overtime
          break
        case '补卡':
          return FillClock
          break
        case '转正':
          return Regularization
          break
        case '调岗':
          return TransferPosition
          break
        case '离职':
          return Dimission
          break
        case '调薪':
          return SalaryAdjustment
          break
        case '报销':
          return Certify
          break
      }
    },

    // 加载更多
    getMore(type) {
      switch (type) {
        case 'iInitiatedList':
          // 我发起的审批加载更多
          this.iInitiatedCurrentPage++
          // 筛选 发起审批
          InstanceQuery(
            {
              jobNumber: this.$store.state.userInfo.jobNumber, //工号
              processKey: '', //流程标识
              processState: this.selectStateList.indexOf(this.selectStateValue), //审批状态
              processName:
                this.selectTypeValue1 === '全部类型'
                  ? ''
                  : this.selectTypeValue1,
              departmentName: this.$store.state.postInfo.department,
              procCurrNodeUserGroup: '', //当前节点负责人角色
              currentPage: this.iInitiatedCurrentPage,
              pageSize: 10,
            },
            this.$store.state.authorization
          ).then((res) => {
            // console.log('查询我发起的', res.data.rows)
            this.iInitiatedList.push(...res.data.rows)
            this.showGetMore.iInitiatedList =
              res.data.total > this.currentPage * 10 ? true : false
          })

          break
        case 'myApprovalList':
          // 我的审批加载更多
          this.myApprovalCurrentPage++
          // 筛选 我的审批
          InstanceQuery(
            {
              jobNumber: '', //工号
              processKey: '', //流程标识
              processState: 0, //审批状态
              processName:
                this.selectTypeValue2 === '全部类型'
                  ? ''
                  : this.selectTypeValue2,
              departmentName: this.$store.state.postInfo.department,
              procCurrNodeUserGroup: this.$store.state.role, //当前节点负责人角色
              currentPage: this.myApprovalCurrentPage,
              pageSize: 10,
            },
            this.$store.state.authorization
          ).then((res) => {
            // console.log('我的审批', res.data.rows)
            this.myApprovalList.push(...res.data.rows)
            this.showGetMore.myApprovalList =
              res.data.total > this.currentPage * 10 ? true : false
          })
          break
      }
    },
  },
}
